import {Project} from "@/mysql/project"
const {ProjectType} = require('@/mysql/project_type')
const {sequelize} = require('./utils')
const Sequelize = require('sequelize')
const {UserProject} = require('@/mysql/user_project')
const Op = Sequelize.Op
// 创建用户实体
const User = sequelize.define('users', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: Sequelize.STRING,
        unique: true
    },
    password: {
        type: Sequelize.STRING
    },
    company: {
        type: Sequelize.STRING
    },
    avatarImage: {
        type: Sequelize.STRING
    },
    sex: {
        type: Sequelize.INTEGER
    },
    age: {
        type: Sequelize.INTEGER
    },
    desc: {
        type: Sequelize.STRING
    },
    email: {
        type: Sequelize.STRING,
        validate: {
            isEmail: true
        }
    },
    phone: {
        type: Sequelize.STRING,
        validate: {
            isNumeric: true
        }
    }
})
User.sync({force: false})
/** 插入新用户数据（注册） */
User.insertData = async (name, password) => {
    await User.sync({force: false})
    // 表已创建
    return User.create({
        name: name,
        password: password
    })
}
/** 查询是否有相同用户名 */
User.findName = function (name) {
    return User.findAll({
        where: {
            name: name
        }
    })
}

User.findById = async (id) => {
    return User.findAll({where: {id}})
}
/** 添加userProject */
User.addProject = async (name, description, type_id) => {
    // force: true 如果表已经存在，将会丢弃表
    await Project.sync({force: false})
    const project = await Project.create({name, description, projectTypeTypeId: type_id});
    const userId = global.utils.PersonInfo.data[0].id
    return UserProject.insertData(userId, project.project_id, true)
}
/** 用户修改项目 */
User.updateProject = async (name, description, type_id, project_id) => {
    // force: true 如果表已经存在，将会丢弃表
    await Project.sync({force: false})
    return Project.update({name, description, projectTypeTypeId: type_id},{where: {project_id}});
}
/** 用户删除项目 */
User.removeProject = async (project_id) => {
    return Project.update({deleted: true}, {where: {project_id}})
}
/** 用户删除项目 */
User.returnProject = async (project_id) => {
    return Project.update({deleted: false}, {where: {project_id}})
}
/**
 * @functionName: 获取当前登录用户所有项目
 * @param:  User
 * @date 2019/5/19 22:36
 * @description 如上
 * @version 1.0
 */
User.getProject = async (deleted = false, projectTypeTypeId = null, name = null) => {
    await Project.sync({force: false})
    const userId = global.utils.PersonInfo.data[0].id
    const myProjectIds = await UserProject.findAll({attributes: ['project_id'], where: {user_id: userId}})
    const arr = []
    myProjectIds.forEach(e => {
        arr.push(e.project_id)
    })
    return await Project.findAll({
        where: {
            name: name? name: {
                [Op.not]: '-1'
            },
            deleted: Boolean(deleted === 'true'),
            projectTypeTypeId: projectTypeTypeId? projectTypeTypeId: {
                [Op.not]: -1
            },
            project_id: {
                [Op.in]: arr
            }
        },
        include:[{
            model: ProjectType,
            attributes: ['name', 'type_id'],
            where: {type_id: Sequelize.col('projectTypeTypeId')}
        }]
    })
}
/** 通过项目Id获取用户信息 */
User.findByProjectId = async (project_id) => {
    const data = await UserProject.findAll({where: {project_id}})
    const arr =[]
    data.forEach(element => {
        arr.push(element.user_id)
    })
    const users =  await User.findAll({where: {id: {
                [Op.in]: arr
            }}})
    data.forEach(element=> {
        users.forEach(user => {
            if(element.user_id == user.id) {
              element.dataValues.name = user.name}
        })
    })
    return data
}

User.updateUerInfo = async (id, name, desc, avatarImage, sex, age, phone, email, company) => {
    return User.update({name, desc, avatarImage, sex, age, phone, email, company}, {where: {id}})
}
export {User}
